package com.sky.service.impl;


import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.exception.LoginFailedException;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.ShoppingParamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long dishId = shoppingCartDTO.getDishId();
        List<ShoppingCart> shoppingCartList;

        //1.获取用户ID
        Long currentId = BaseContext.getCurrentId();
        if (currentId == null) {
            throw new LoginFailedException(MessageConstant.USER_NOT_LOGIN);
        }

        //2.判断该用户是否添加过购物项
        ShoppingCart condition = ShoppingCart.builder()
                .userId(currentId)
                .dishId(dishId)
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .setmealId(shoppingCartDTO.getSetmealId())
                .build();
        shoppingCartList = shoppingCartMapper.findShoppingCartByCondition(condition);
        /*if (dishId != null) {
            //2.1判断该购物项是菜品购物项{dishFlavor:"泉塘"，dishId:102}
            //根据用户ID 菜品ID 口味
            ShoppingCart condition = ShoppingCart.builder()
                    .userId(currentId)
                    .dishId(dishId)
                    .setmealId(shoppingCartDTO.getSetmealId())
                    .dishFlavor(shoppingCartDTO.getDishFlavor()).build();
            shoppingCartList = shoppingCartMapper.findShoppingCartByCondition(condition);
        } else {
            //2.2 套餐购物车处理{setmealId:1}
            //用户ID 套餐ID
            ShoppingCart condition = ShoppingCart.builder()
                    .userId(currentId)
                    .setmealId(shoppingCartDTO.getSetmealId())
                    .build();
            shoppingCartList = shoppingCartMapper.findShoppingCartByCondition(condition);
        }*/
        //判断是否购物过 如果没 那么新增一条购物项 如果有 在原来的基础上+1;
        if (shoppingCartList == null || shoppingCartList.size() <= 0) {
            //新增购物项
            //按照菜品/套餐ID查询 name image price => amount
            ShoppingParamVO paramVO;
            if (dishId != null) {
                paramVO = dishMapper.findByIds(dishId);
            } else {
                paramVO = setmealMapper.findByIds(shoppingCartDTO.getSetmealId());
            }
            ShoppingCart shoppingCart = ShoppingCart.builder()
                    .userId(currentId)
                    .dishId(dishId)
                    .dishFlavor(shoppingCartDTO.getDishFlavor())
                    .setmealId(shoppingCartDTO.getSetmealId())
                    .name(paramVO.getName())
                    .amount(paramVO.getAmount())
                    .image(paramVO.getImage())
                    .number(1)
                    .createTime(LocalDateTime.now())
                    .build();
            shoppingCartMapper.insertShoppingCart(shoppingCart);
        } else {
            //数量加一
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            shoppingCartMapper.changeShoppingCartNum(shoppingCart.getId(), 1);
        }


    }

    @Override
    public List<ShoppingCart> selectShoppingCart() {
        List<ShoppingCart> shoppingCart = shoppingCartMapper.selectShoppingCart();
        return shoppingCart;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //获取用户id判断其id是否为空可以用 @Valid @NotNull
        Long currentId = BaseContext.getCurrentId();
        if (currentId == null) {
            throw new LoginFailedException(MessageConstant.USER_NOT_LOGIN);
        }
        //2.查询用户想要删除的商品数据
        ShoppingCart condition = ShoppingCart.builder()
                .userId(currentId)
                .dishId(shoppingCartDTO.getDishId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .setmealId(shoppingCartDTO.getSetmealId())
                .build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findShoppingCartByCondition(condition);
        //3.判断是否存在，不存在要提示错误信息
        if (shoppingCartList == null || shoppingCartList.size() ==0){
            throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_NOT_EXIST);
        }
        ShoppingCart cart = shoppingCartList.get(0);
        if (cart.getNumber()<=1) {
            Integer integer = shoppingCartMapper.deleteShoppingCart(cart.getId());
        } else if (cart.getNumber()>1){
            //数量减一
            shoppingCartMapper.changeShoppingCartNum(cart.getId(), -1);
        }
    }

    @Override
    public void deleteShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.delete(currentId);
    }


}
